package com.ggb.reactive.controller.user

import com.ggb.reactive.domain.entity.Category
import com.ggb.reactive.extension.log
import com.ggb.reactive.model.WebResult
import com.ggb.reactive.repository.CategoryRepository
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import lombok.RequiredArgsConstructor
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono

@RequestMapping("/user/category")
@RestController("userCategoryController")
@RequiredArgsConstructor
@Tag(name = "分类管理")
class CategoryController(
    val categoryRepository: CategoryRepository,
) {
    @GetMapping("/list")
    @Operation(summary = "获取分类列表")
    fun list():Mono<WebResult<List<Category>>>{
        return categoryRepository.findAll()
            .collectList().map {
            WebResult.success(it)
        }
            .onErrorResume {
                log.error("报错警告 {}",it.message)
                Mono.just(WebResult.error(it.message.toString()))
            }
    }
}